package youleche.com.business;

import java.sql.Connection;
import java.util.List;

import youleche.com.connection.ConnectionFactory;
import youleche.com.factory.DaoFactory;
import youleche.com.idao.IVehicleImageInfo;
import youleche.com.model.MVehicleImageInfo;

public class BVehicleImageInfo {
	/**
	 * 数据库连接
	 */
	private Connection con = null;
	
	/**
	 * 处理器
	 */
	private IVehicleImageInfo handler = null;
	
	/**
	 * 构造器
	 */
	public BVehicleImageInfo(){
		this.handler = DaoFactory.getDVehicleImageInfo();
	}
	
	/**
	 * 添加车辆图片信息
	 * @param model 图片信息
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int addVehicleImageInfo(MVehicleImageInfo model)
			throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.addVehicleImageInfo(this.con, model);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取首页图片信息
	 * @return 返回首页图片信息（只有10条）
	 * @throws Exception 异常
	 */
	public List<MVehicleImageInfo> getHomeImageInfo()
			throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getHomeImageInfo(this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 通过车辆id获取车辆图片信息
	 * @param vehicleId 车辆id
	 * @return 返回图片信息
	 * @throws Exception 异常
	 */
	public List<MVehicleImageInfo> getImageInfoByVehicleId(String vehicleId) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getImageInfoByVehicleId(this.con, vehicleId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据图片类型获取图片信息（DISTINCT后）
	 * @param imageType 图片类型
	 * @param limit 大小
	 * @return 返回图片信息集合
	 * @throws Exception 异常
	 */
	public List<MVehicleImageInfo> getImageByType_DISTINCT(
			String imageType, int limit) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getImageByType_DISTINCT(this.con, imageType, limit);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据车辆信息Id删除图片ID
	 * @param vehicleId 车辆ID
	 * @return 返回图片信息
	 * @throws Exception 异常
	 */
	public int deleteImageByVehicleId(String vehicleId)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.deleteImageByVehicleId(this.con, vehicleId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
}
